package com.wms.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wms.dto.WarehouseAreaLocalDto;
import com.wms.dto.WarehouseAreaLocalPageDto;
import com.wms.dto.WarehouseAreaLocalQueryDto;
import com.wms.mapper.WarehouseAreaLocalMapper;
import com.wms.mapper.WarehouseAreaMapper;
import com.wms.mapper.WarehouseMapper;
import com.wms.pojo.PageResult;
import com.wms.pojo.Warehouse;
import com.wms.pojo.WarehouseArea;
import com.wms.pojo.WarehouseAreaLocal;
import com.wms.service.WarehouseAreaLocalService;
import com.wms.vo.WarehouseAreaLocalVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class WarehouseAreaLocalServiceImpl extends ServiceImpl<WarehouseAreaLocalMapper, WarehouseAreaLocal> implements WarehouseAreaLocalService {

    @Autowired
    private WarehouseAreaMapper areaMapper;
    @Autowired
    private WarehouseMapper warehouseMapper;

    @Override
    public PageResult queryPage(WarehouseAreaLocalPageDto dto) {
        PageHelper.startPage(dto.getPage(), dto.getPageSize());
        List<WarehouseAreaLocal> warehouseAreaLocals = getBaseMapper().queryPage(dto);
        Page<WarehouseAreaLocal> list = (Page<WarehouseAreaLocal>) warehouseAreaLocals;
        return new PageResult(list.getTotal(), list.getResult());
    }

    @Override
    public void saveWarehouseAreaLocal(WarehouseAreaLocalDto dto) {
        WarehouseAreaLocal warehouseAreaLocal = WarehouseAreaLocal.builder().warehouseId(dto.getWarehouseId())
                .warehouseAreaId(dto.getWarehouseAreaId())
                .localCode(dto.getLocalCode())
                .localLength(dto.getLocalLength())
                .localVolume(dto.getLocalVolume())
                .localWidth(dto.getLocalWidth())
                .localHeight(dto.getLocalHeight())
                .localBearingCapacity(dto.getLocalBearingCapacity())
                .createTime(LocalDateTime.now())
                .updateTime(LocalDateTime.now())
                .tunnelNumber(dto.getTunnelNumber())
                .layerNumber(dto.getLayerNumber())
                .shelfNumber(dto.getShelfNumber())
                .status(dto.getStatus())
                .build();
        getBaseMapper().insert(warehouseAreaLocal);
    }

    @Override
    public void updateWarehouseAreaLocalById(WarehouseAreaLocalDto dto) {
        WarehouseAreaLocal warehouseAreaLocal = WarehouseAreaLocal.builder()
                .id(dto.getId())
                .warehouseId(dto.getWarehouseId())
                .warehouseAreaId(dto.getWarehouseAreaId())
                .localCode(dto.getLocalCode())
                .localLength(dto.getLocalLength())
                .localVolume(dto.getLocalVolume())
                .localWidth(dto.getLocalWidth())
                .localHeight(dto.getLocalHeight())
                .localBearingCapacity(dto.getLocalBearingCapacity())
                .updateTime(LocalDateTime.now())
                .tunnelNumber(dto.getTunnelNumber())
                .layerNumber(dto.getLayerNumber())
                .shelfNumber(dto.getShelfNumber())
                .status(dto.getStatus())
                .build();
        getBaseMapper().updateById(warehouseAreaLocal);
    }

    @Override
    public List<WarehouseAreaLocal> getlist() {
        List<WarehouseAreaLocal> list = lambdaQuery().list();
        for (WarehouseAreaLocal local : list) {
            Warehouse warehouse = warehouseMapper.selectById(local.getWarehouseId());
            WarehouseArea warehouseArea = areaMapper.selectById(local.getWarehouseAreaId());
            local.setWarehouseName(warehouse.getName());
            local.setWarehouseAreaName(warehouseArea.getName());
        }
        return list;
    }

    @Override
    public PageResult getWarehouseAreaLocaByPage(WarehouseAreaLocalQueryDto dto) {
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        List<WarehouseAreaLocalVo> list = baseMapper.findByPage(dto);
        Page<WarehouseAreaLocalVo> p = (Page<WarehouseAreaLocalVo>) list;
        return new PageResult(p.getTotal(), p.getResult());
    }
}
